@extends('admin.admin')

@section('content')
@inject('pictureService', 'App\Interfaces\PictureService')

<?php

Form::macro(
        'selectRequired',
        function($name, $options = [], $selected = null, $attributes = [], $disabled = [])
        {
            $html = '<select name="' . $name . '"';
            foreach ($attributes as $attribute => $value)
            {
                $html .= ' ' . $attribute . '="' . $value . '"';
            }
            $html .= '>';

            foreach ($options as $value => $text)
            {
                $html .= '<option value="' . $value . '"' .
                        ($value == $selected ? ' selected="selected"' : '') .
                        (in_array($value, $disabled) ? ' disabled="disabled"' : '') . '>' .
                        $text . '</option>';
            }

            $html .= '</select>';

            return $html;
        }
);?>
        <!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        品牌管理
        <small>品牌列表</small>
        <small>修改</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 控制面板</a></li>
        <li >品牌管理</li>
        <li ><a href="/admin/brand">品牌列表</a></li>
        <li class="active">修改</li>
    </ol>
</section>

<!-- Main content -->
<section class="content">

    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">品牌修改</h3>

                    <div class="pull-right">
                        <a href="{{Route('admin.brand.index')}}" type="button" class="btn btn-block btn-success btn-xs"><i
                                    class="icon  glyphicon glyphicon glyphicon-menu-left"></i> 返回</a>
                    </div>
                </div>
                <!-- /.box-header -->
                <!-- form start -->
                {!! Form::model($brand, ['route'=>['admin.brand.update', $brand->brand_id], 'method'=>'put','class'=>'form-horizontal'])  !!}
                {!! Form::hidden('back_url', @$_SERVER['HTTP_REFERER']) !!}
                <div class="box-body">
                    <div class="form-group @if($errors->has('brand_name')) has-error @endif">
                        <label for="exampleInputEmail1" class="col-sm-2 control-label">品牌名称</label>
                        <div class="col-sm-3">
                            {!! Form::text('brand_name', old('brand_name'), array('class'=>'form-control','placeholder'=>'品牌名称'))!!}
                            @if($errors->has('brand_name'))
                                <label class="control-label" for="inputError"><i
                                            class="fa fa-times-circle-o"></i> {{$errors->first('brand_name')}}</label>
                            @endif
                        </div>
                    </div>
                    <div class="form-group @if($errors->has('brand_logo')) has-error   @endif">
                        <label for="exampleInputPassword1" class="col-sm-2 control-label">品牌logo</label>

                        <div class="col-sm-3">
                            <div style="width: 170px;">
                                <div class="image-uploader">
                                    <style>
                                        .upload-btn{
                                            display: block; width:48px;height:30px;position: absolute;filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity: 0;opacity: 0;
                                        }
                                    </style>
                                    <!-- /.box-header -->
                                    <div class="box-body">

                                        <div style="width: 145px; display: block;">
                                            <img class="image" style="padding: 1px;height: auto;" width="100%" src="@if (old('brand_logo',$brand->brand_logo)) {{$pictureService::thumb(old('brand_logo',$brand->brand_logo))}} @else http://placehold.it/156x60?text=56x38 @endif"/>
                                            <input type="hidden" id="image_url" name="image_url" value="{{old('image_url')}}">
                                        </div>
                                        <div class="message" style="width: 145px;height: 145px; display: block;position: absolute; padding: 5px; top:0px; color: red; text-align: center; line-height: 145px;display: none;">

                                        </div>

                                        <div class="progress progress-xxs" style="position: absolute;width: 140px; left:13px; display: none;">
                                            <div class="progress-bar progress-bar-default " role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
                                            </div>
                                        </div>
                                    </div>
                                    <!-- /.box-body -->
                                    <div class="box-footer" style="position: relative">

                                        <button class="btn btn-sm btn-default btn-flat pull-left" type="button">上传</button>
                                        {!! Form::file('file',['class'=>'upload-btn','id'=>'fileupload','type'=>'file','data-url'=>'/admin/file_upload']) !!}
                                        {!! Form::hidden('brand_logo',old('brand_logo'),['id'=>'thumb']) !!}

                                        <button class="btn  btn-sm btn-danger btn-flat pull-right" type="button">删除</button>
                                    </div>
                                </div>
                                <!-- /.box -->
                            </div>
                        </div>

                        @if($errors->has('brand_logo'))
                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('brand_logo')}}</label>
                        @endif
                    </div>

                    <div class="form-group @if($errors->has('m_brand_logo')) has-error   @endif">
                        <label for="exampleInputPassword1" class="col-sm-2 control-label">手机端品牌logo</label>

                        <div class="col-sm-3">
                            <div style="width: 170px;">
                                <div class="image-uploader">
                                    <style>
                                        .upload-btn{
                                            display: block; width:48px;height:30px;position: absolute;filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity: 0;opacity: 0;
                                        }
                                    </style>
                                    <!-- /.box-header -->
                                    <div class="box-body">

                                        <div style="width: 145px; display: block;">
                                            <img class="image" style="padding: 1px;height: auto;" width="100%" src="@if (old('m_brand_logo',$brand->m_brand_logo)) {{$pictureService::thumb(old('m_brand_logo',$brand->m_brand_logo))}} @else http://placehold.it/156x60?text=56x38 @endif"/>
                                            <input type="hidden" id="image_url" name="image_url" value="{{old('image_url')}}">
                                        </div>
                                        <div class="message" style="width: 145px;height: 145px; display: block;position: absolute; padding: 5px; top:0px; color: red; text-align: center; line-height: 145px;display: none;">

                                        </div>

                                        <div class="progress progress-xxs" style="position: absolute;width: 140px; left:13px; display: none;">
                                            <div class="progress-bar progress-bar-default " role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
                                            </div>
                                        </div>
                                    </div>
                                    <!-- /.box-body -->
                                    <div class="box-footer" style="position: relative">

                                        <button class="btn btn-sm btn-default btn-flat pull-left" type="button">上传</button>
                                        {!! Form::file('file',['class'=>'upload-btn','id'=>'fileupload','type'=>'file','data-url'=>'/admin/file_upload']) !!}
                                        {!! Form::hidden('m_brand_logo',old('m_brand_logo'),['id'=>'thumb']) !!}

                                        <button class="btn  btn-sm btn-danger btn-flat pull-right" type="button">删除</button>
                                    </div>
                                </div>
                                <!-- /.box -->
                            </div>
                        </div>

                        @if($errors->has('brand_logo'))
                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('brand_logo')}}</label>
                        @endif
                    </div>


                    <?php $select = $category::getTreeList();?>

                    <?php

                    function myspace($n,$hasChild){
                        $enum = ['├','└'];
                        if ($hasChild){
                            $space = $enum[0];
                        }else{
                            $space = $enum[1];
                        }
                        $str = '';
                        for ($i=0;$i<=$n;$i++){
                            $str.='&nbsp;&nbsp;';
                        }
                        return $str.$space;
                    }
                    $seletcItem[0] = '=请选择商品分类=';
                    $disabledOption = [];
                    foreach ($select as $k=> $item){
                        if($item->has_children){
                            $disabledOption[]=$item->cat_id;
                        }
                        $seletcItem [$item->cat_id]= myspace($item->deepth,$item->has_children).$item->cat_name;
                    }

                    ?>



                    <div class="form-group">
                        <label for="exampleInputPassword1" class="col-sm-2 control-label">描述</label>
                        <div class="col-sm-3">
                            {!! Form::textarea('brand_desc',old('brand_desc'),array('class'=>'form-control','placeholder'=>'请填写描述','rows'=>3))!!}
                            @if($errors->has('brand_desc'))
                                <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('brand_desc')}}</label>
                            @endif
                        </div>
                    </div>
                </div>
                <!-- /.box-body -->
                <div class="box-footer">
                    <div class="form-group no-padding">
                        <label class="col-sm-2 control-label">&nbsp;</label>
                        <div class="col-sm-3">
                            <button type="submit" class="btn btn-primary">保存</button>
                        </div>
                    </div>
                </div>
                {!! Form::close()  !!}
            </div>
            <!-- /.box -->
        </div>
    </div>


</section>
<script src="{{ asset ("/components/blueimp-file-upload/js/vendor/jquery.ui.widget.js") }}"></script>
<script src="{{ asset ("/components/blueimp-file-upload/js/jquery.iframe-transport.js") }}"></script>
<script src="{{ asset ("/components/blueimp-file-upload/js/jquery.fileupload.js") }}"></script>
<script>
    $(function () {
        $('.image-uploader').each(function(k,imageUploader){
            var wrapper = $(imageUploader);//盒子
            var uploadBtn = wrapper.find('#fileupload');
            var imageFile = wrapper.find('.image');
            var progress = wrapper.find('.progress');
            var progressBar = progress.find('.progress-bar');
            var inputFile = wrapper.find('#thumb');
            var imageUrl = wrapper.find('#image_url');
            var message = wrapper.find('.message');

            uploadBtn.click(function(){
                progressBar.css('width', 0 + '%');
                imageFile.attr('src','http://placehold.it/156x60?text=56x38');
                inputFile.val('');
                message.hide();

            });
            wrapper.find('.btn-danger').click(function(){
                progressBar.css('width', 0 + '%');
                imageFile.attr('src','http://placehold.it/156x60?text=56x38');
                inputFile.val('');
                message.hide();

            });

            uploadBtn.fileupload({
                dataType: 'json',
                headers:{'X-CSRF-TOKEN':'{{csrf_token()}}'},
                add: function (e, data) {
                    data.submit();
                },
                progressall: function (e, data) {
                    progress.fadeIn();
                    var percent = parseInt(data.loaded / data.total * 100, 10);
                    if (progress==1){
                        progress.show()
                    }
                    progressBar.css('width', percent + '%');
                    setTimeout(function () {
                        progress.fadeOut();
                    }, 500);
                },
                done: function (e, data) {
                    imageFile.attr('src',data.result.url);
                    inputFile.val(data.result.id);
                    imageUrl.val(data.result.url);
                }
            });
        });
    });
</script>
<!-- /.content -->
@endsection